package com.zy.scwwebuis.controller;

import com.zy.scwcommon.bean.AppResponse;
import com.zy.scwcommon.vo.user.UserAddressVo;
import com.zy.scwwebuis.service.TMemberServiceFeign;
import com.zy.scwwebuis.service.TProjectInfoServiceFeign;
import com.zy.scwwebuis.vo.resp.ProjectDetailVo;
import com.zy.scwwebuis.vo.resp.ReturnPayConfirmVo;
import com.zy.scwwebuis.vo.resp.UserRespVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
* @program: zyzc_springcloud
*
* @description: 获取项目信息
*
* @author: ZYY
*
* @create: 2024-07-10 09:58
**/
@Controller
public class ProjectInfoController {
    @Autowired
    TProjectInfoServiceFeign tProjectInfoServiceFeign;
    @Autowired
    TMemberServiceFeign tMemberServiceFeign;
    /**
     * @Description:远程调用-----支持接口的调用
     * @Param:根据项目ID和回报的ID查询数据
     * @return: 支持的页面
     * @Author: zy
     * @Date:
     */
    @RequestMapping("/project/support/{projectId}/{returnId}")
    public String support(@PathVariable("projectId") Integer projectId,
                          @PathVariable("returnId") Integer returnId, Model model,HttpSession session){
       //根据项目ID和回报的ID查询数据
        AppResponse<ReturnPayConfirmVo> support = tProjectInfoServiceFeign.support(projectId, returnId);
   ReturnPayConfirmVo returnPayConfirmVo = support.getData();
    model.addAttribute("returnPayConfirmVo",returnPayConfirmVo);
        session.setAttribute("returnPayConfirmVoSession", returnPayConfirmVo);

        return "project/pay-step-1";


    }
    /**
     * @Description:远程调用----项目ID查出项目详细信息
     * @Param:
     * @return:
     * @Author: zy
     * @Date:
     */
    @RequestMapping("/project/projectInfo")
    public String index(Integer id , Model model){
        AppResponse<ProjectDetailVo> detail = tProjectInfoServiceFeign.detailsInfo(id);
        ProjectDetailVo data = detail.getData();
        //todo  访问量过大的话可以放到 reids中减少数据的压力
        model.addAttribute("projectDetailVo",data);
        return "project/index";
    }
/**
 * @Description:调用去结算的页面---的获取用户地址接口
 * @Param:
 * @return:
 * @Author: zy
 * @Date:
 */
@ApiOperation(value="获取用户收货地址")
@ApiImplicitParams(value={
        @ApiImplicitParam(value="访问令牌",name="accessToken",required=true)
})
@GetMapping("/project/confirm/order/{num}")
public String address(@PathVariable("num") Integer num, Model model,HttpSession session){
//1.数据展示 - 地址信息
    UserRespVo userRespVo  = (UserRespVo)session.getAttribute("loginMember");

    if(userRespVo == null) {
        session.setAttribute("preUrl","/project/confirm/order/"+num);
        return "redirect:/login";
    }

    String accessToken = userRespVo.getAccessToken();

    AppResponse<List<UserAddressVo>> resp = tMemberServiceFeign.address(accessToken);
    List<UserAddressVo> list = resp.getData();
    model.addAttribute("memberAddressList", list);


    //2.数据展示 - 回报信息（和上一个页面共享session数据）
    ReturnPayConfirmVo returnPayConfirmVo = (ReturnPayConfirmVo)session.getAttribute("returnPayConfirmVoSession");

    returnPayConfirmVo.setNum(num);
    returnPayConfirmVo.setTotalPrice(new BigDecimal(num * returnPayConfirmVo.getPrice()+returnPayConfirmVo.getFreight()));

    session.setAttribute("returnPayConfirmVoSession", returnPayConfirmVo);

    return "project/pay-step-2";
}
}
